package com.yu.controller;

import java.io.File;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yu.bean.Msg;
import com.yu.bean.Tpicture;
import com.yu.service.TpictureService;

@Controller
public class TpictureController {

	@Autowired
	TpictureService tpictureService;
	
	/**
	 * 标题图片上传
	 * @param file
	 * @param id
	 * @param request
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/uploadtImg", method=RequestMethod.POST)
    public Msg uploadTpic(
    		@RequestParam(value="file",required=false)MultipartFile file,
            @RequestParam(value="id", required=false) String id,
            HttpServletRequest request){
    	//String selectedId = request.getParameter("selectedId");
        File targetFile=null;
        String storUrl="";//返回存储路径
        Tpicture tpic = null;
        System.out.println("开始上传标题图片=========>"+file);
        if(file!= null && !file.equals("") ){
        		String fileName=file.getOriginalFilename();//获取文件名加后缀 
	            String returnUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() +"/upload/imgs/";//存储路径
	            String path = request.getSession().getServletContext().getRealPath("upload/imgs"); //文件存储位置
	            String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());//文件后缀
	            fileName=new Date().getTime()+"_"+new Random().nextInt(1000)+fileF;//新的文件名
	            
	            //
	            File file1 =new File(path); 
	            System.out.println("标题图片的上传路径为====>："+path);
	            //如果文件夹不存在则创建    
	            if(!file1 .exists()  && !file1 .isDirectory()){       
	                file1 .mkdir();  
	            }
	            targetFile = new File(file1, fileName);
                //targetFile = new File(path, fileName);
	            try {
	                file.transferTo(targetFile);
                   //msg = returnUrl + fileName;
	               // msg=returnUrl+fileAdd+"/"+fileName;
	                storUrl=returnUrl+fileName;
	                System.out.println("标题图片的返回路径为====》"+storUrl);
	                tpictureService.uploadtImgs(id,storUrl);
	                tpic = tpictureService.selectTpicByUrl(storUrl);
	                System.out.println("上传成功====>"+storUrl);
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
        }
        return Msg.success().add("tpic", tpic);
	}
	
	
	/**
	 * 删除标题图片
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/delTpic/{delId}", method=RequestMethod.DELETE)
	public Msg delTpic(@PathVariable("delId")String delId){
		Integer dId = Integer.parseInt(delId);
		tpictureService.delTpicById(dId);
		return Msg.success().add("msg", "数据图片删除成功！");
		
	}
}
